﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using DiamondDesktop.Presenters;
using DiamondDesktop.Model;
using DiamondDesktop.type;
using System.Threading;
using DiamondDesktop.utility;
namespace DiamondDesktop
{
	public partial class Window1:Window
	{
        Reminder r;
        private SAPISpeaker speaker = new SAPISpeaker();   //

		public Window1()
		{

            InitializeComponent();
            this.Left = 780;
            this.Top = 95;
            ApplicationController controller = ApplicationController.getInstance();
            r = Reminder.getInstance();
            r.addObserver(new notifyObserver(update));  //注册Reminder的观察者
            controller.setShell(this);
            DataContext = controller;
			// 在此点之下插入创建对象所需的代码。
            speaker.Speak("hello，welcome to diamond reminder。");
		    
        }

        public void TransitionTo(object view)
        {
            currentView.Content = view;
        }

                
        public ApplicationController Controller
        {
            get { return (ApplicationController)DataContext; }
        }


        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void WindowMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Newtask_Click(object sender, RoutedEventArgs e)
        {
            Controller.ShowNewtask();
            Button_taskcount.IsEnabled = true;
            Button_new.IsEnabled = false;
            Button_tasklist.IsEnabled = true;
            Button_gantt.IsEnabled = true;
            Button_setr.IsEnabled = true;
            Button_sets.IsEnabled = true;

        }

        private void Count_Click(object sender, RoutedEventArgs e)
        {
            Controller.ShowCount();
            Button_taskcount.IsEnabled = false;
            Button_new.IsEnabled = true;
            Button_tasklist.IsEnabled = true;
            Button_gantt.IsEnabled = true;
            Button_setr.IsEnabled = true;
            Button_sets.IsEnabled = true;
        }

        private void TaskList_Click(object sender, RoutedEventArgs e)
        {
            Controller.ShowTaskList();
            Button_taskcount.IsEnabled = true;
            Button_new.IsEnabled = true;
            Button_tasklist.IsEnabled = false;
            Button_gantt.IsEnabled = true;
            Button_setr.IsEnabled = true;
            Button_sets.IsEnabled = true;
        }

        private void TaskGantt_Click(object sender, RoutedEventArgs e)
        {
            Controller.ShowTaskGantt();
            Button_taskcount.IsEnabled = true;
            Button_new.IsEnabled = true;
            Button_tasklist.IsEnabled = true;
            Button_gantt.IsEnabled = false;
            Button_setr.IsEnabled = true;
            Button_sets.IsEnabled = true;
        }

        private void SetReminder_Click(object sender, RoutedEventArgs e)
        {
            Controller.ShowSetReminder();
            Button_taskcount.IsEnabled = true;
            Button_new.IsEnabled = true;
            Button_tasklist.IsEnabled = true;
            Button_gantt.IsEnabled = true;
            Button_setr.IsEnabled = false;
            Button_sets.IsEnabled = true;
        }

        private void SetSystem_Click(object sender, RoutedEventArgs e)
        {
            Controller.ShowSetSystem();
            Button_taskcount.IsEnabled = true;
            Button_new.IsEnabled = true;
            Button_tasklist.IsEnabled = true;
            Button_gantt.IsEnabled = true;
            Button_setr.IsEnabled = true;
            Button_sets.IsEnabled = false;
        }

        //根据消息类型，执行不同的操纵
        private void update(Object o)
        {
            NotifyType t = (NotifyType)o;
            switch (t)
            {
                case NotifyType.REMIND:           //消息类型为提醒
                    showReminderWindow();
                    break;
                case NotifyType.UPDATE:
                    Controller.ShowTaskList();
                    break;
            }
        }
    
        //显示提醒窗口
        private void showReminderWindow()
        {
            Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start();

        }

        private void ThreadStartingPoint()
        {
            Task t = r.getTaskToRemind();
            new ReminderWindow(t).Show();
            System.Windows.Threading.Dispatcher.Run();
        }

        private void WMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.DragMove();
        }

	}
}